package com.lzb.permanentcalendar.adapter;


import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.github.promeg.pinyinhelper.Pinyin;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.bean.CityBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_CITY = 1;

    public List<Object> dataList = new ArrayList<>();
    private Map<String, Integer> letterPositions = new HashMap<>();
    private OnItemClickListener onItemClickListener;

    public void setData(List<CityBean> validCities) {
        dataList.clear();
        letterPositions.clear();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // 分离热门城市
            List<CityBean> hotCities = new ArrayList<>();
            List<CityBean> normalCities = new ArrayList<>();
            for (CityBean city : validCities) {
                if (city.getCityName().contains("北京") || city.getCityName().contains("上海") || city.getCityName().contains("深圳") || city.getCityName().contains("广州") || city.getCityName().contains("杭州") || city.getCityName().contains("南京")) {
                    hotCities.add(city);
                } else {
                    normalCities.add(city);
                }
            }

            // 添加热门城市区块
            if (!hotCities.isEmpty()) {
                dataList.add("热门城市"); // 热门区块标题
                dataList.addAll(hotCities);
            }


            // 按拼音排序
            Collections.sort(validCities, (c1, c2) ->
                    Pinyin.toPinyin(c1.getCityName().charAt(0)).substring(0, 1)
                            .compareTo(Pinyin.toPinyin(c2.getCityName().charAt(0)).substring(0, 1)));
            CityBean chongqingCity = null;
            for (int i = 0; i < validCities.size(); i++) {
                CityBean city = validCities.get(i);
                if (city.getCityName().contains("重庆")) {
                    chongqingCity = validCities.remove(i);
                    break;
                }
            }

            if (chongqingCity != null) {
                for (int i = 0; i < validCities.size(); i++) {
                    CityBean city = validCities.get(i);
                    String letter = Pinyin.toPinyin(city.getCityName().charAt(0)).substring(0, 1);
                    if (letter.equalsIgnoreCase("C")) {
                        validCities.add(i + 1, chongqingCity);
                        break;
                    }
                }
            }
            CityBean chongqingCity1 = null;
            for (int i = 0; i < validCities.size(); i++) {
                CityBean city = validCities.get(i);
                if (city.getCityName().contains("长春")) {
                    chongqingCity1 = validCities.remove(i);
                    break;
                }
            }

            if (chongqingCity1 != null) {
                for (int i = 0; i < validCities.size(); i++) {
                    CityBean city = validCities.get(i);
                    String letter = Pinyin.toPinyin(city.getCityName().charAt(0)).substring(0, 1);
                    if (letter.equalsIgnoreCase("C")) {
                        validCities.add(i + 1, chongqingCity1);
                        break;
                    }
                }
            }
            CityBean chongqingCity2 = null;
            for (int i = 0; i < validCities.size(); i++) {
                CityBean city = validCities.get(i);
                if (city.getCityName().contains("长沙")) {
                    chongqingCity2 = validCities.remove(i);
                    break;
                }
            }

            if (chongqingCity2 != null) {
                for (int i = 0; i < validCities.size(); i++) {
                    CityBean city = validCities.get(i);
                    String letter = Pinyin.toPinyin(city.getCityName().charAt(0)).substring(0, 1);
                    if (letter.equalsIgnoreCase("C")) {
                        validCities.add(i + 1, chongqingCity2);
                        break;
                    }
                }
            }

            // 分组
            String currentLetter = "";
            for (CityBean city : validCities) {
                String letter = Pinyin.toPinyin(city.getCityName().charAt(0)).substring(0, 1);
                if (city.getCityName().contains("重庆") || city.getCityName().contains("长春")|| city.getCityName().contains("长沙")) {
                    letter = "C";
                } else {
                    letter = Pinyin.toPinyin(city.getCityName().charAt(0)).substring(0, 1);
                }

                if (!letter.equals(currentLetter)) {
                    currentLetter = letter;
                    dataList.add(currentLetter);
                    letterPositions.put(currentLetter, dataList.size() - 1);
                }
                dataList.add(city);
            }
            notifyDataSetChanged();
        }

    }


    @Override
    public int getItemViewType(int position) {
        return dataList.get(position) instanceof String ? TYPE_HEADER : TYPE_CITY;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_header, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_city, parent, false);
            return new CityViewHolder(view);
        }
    }

    public interface OnItemClickListener {
        void OnClick(CityBean bean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            TextView tvHeader = ((HeaderViewHolder) holder).tvHeader;
            tvHeader.setText((String) dataList.get(position));
        } else {
            CityBean city = (CityBean) dataList.get(position);
            TextView tvCity = ((CityViewHolder) holder).tvCity;
            tvCity.setText(city.getCityName());
            tvCity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.OnClick(city);
                    }
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public int getLetterPosition(String letter) {
        Integer pos = letterPositions.get(letter);
        return pos != null ? pos : -1;
    }

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView tvHeader;

        HeaderViewHolder(View itemView) {
            super(itemView);
            tvHeader = itemView.findViewById(R.id.tvHeader);
        }
    }

    static class CityViewHolder extends RecyclerView.ViewHolder {
        TextView tvCity;

        CityViewHolder(View itemView) {
            super(itemView);
            tvCity = itemView.findViewById(R.id.tvCity);
        }
    }
}